using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FutureOpen.Core;
using FutureOpen.Core.func;
using FutureOpen.Core.func.db;
using System.Collections.Generic;
using FutureOpen.Web.Pages;


public partial class ArtList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.PageInit();
        if (!base.IsLogin())
        {
            base.SimulateLogin();
        }

        if (!IsPostBack)
        {
            AspNetPager1.RecordCount = new MyArt().GetAllArtsCount();
            BindArts(0, 4);
        }
    }
    private void BindArts(int start, int count)
    {
        MyArt myart = new MyArt();
        System.Collections.Generic.IList<Art> artList = myart.GetArtByRange(start, count);

        GridView1.DataSource = artList;
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (base.IsLogin())
        {
            int artid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
            Art art = new Art();
            art.Art_id = artid;
            MyArt myart = new MyArt();
            int code = myart.DelArt(art);
            if (code == StateCode.DELART_OK)
            {
                lblTip.Text = "successfully";
                AspNetPager1.RecordCount = new MyArt().GetAllArtsCount();
                BindArts(4 * (AspNetPager1.CurrentPageIndex - 1), 4);
            }
            else
            {
                lblTip.Text = "failed";
            }
        }
        else
        {
            lblTip.Text = "Login first!";
        }
    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindArts(4 * (e.NewPageIndex - 1), 4);
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            Label lblCat = (Label)GridView1.Rows[i].FindControl("lblCatName");
            DropDownList ddlField = GridView1.Rows[i].FindControl("ddlFields") as DropDownList;
            BindFields(ddlField, Int32.Parse(lblCat.Text));

            HiddenField hdUsr = (HiddenField)GridView1.Rows[i].FindControl("hdUsr");
            HyperLink lblUser = (HyperLink)GridView1.Rows[i].FindControl("lblUser");
            lblUser.Attributes.Add("onmouseover", "ShowInfo(\"GetUserInfo.ashx?UsrID=" + hdUsr.Value + "\")");
        }
    }
    private IList<Cat> list = null;
    private MyCat mycat = null;
    private void BindFields(DropDownList ddlField, int id)
    {
        if (list == null)
        {
            mycat = new MyCat();
            list = mycat.GetChildren(0);
        }

        if (ViewState["FieldsList"] == null)
        {

            ViewState["FieldsList"] = list;
        }
        else
        {
            list = ViewState["FieldsList"] as System.Collections.Generic.IList<Cat>;
        }

        if (list != null)
        {
            ddlField.Items.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                Cat cat = list[i];
                ListItem item = new ListItem(cat.Cat_name, cat.Cat_id.ToString());
                if (cat.Cat_id == id)
                {
                    item.Selected = true;
                }
                ddlField.Items.Add(item);
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (base.IsLogin())
        {
            LinkButton lnkBtn = GridView1.SelectedRow.FindControl("lnkbtnSaveCate") as LinkButton;
            int artid = Convert.ToInt32(lnkBtn.CommandArgument);

            MyArt myart = new MyArt();
            Art art = myart.GetArtByID(artid);

            DropDownList ddlField = GridView1.SelectedRow.FindControl("ddlFields") as DropDownList;
            art.Art_cat = Convert.ToInt32(ddlField.SelectedValue);

            myart.UpdateArt(art);
        }
        else
        {
            lblTip.Text = "Login first!";
        }
    }

    public string GetUserInfo(object obj)
    {
        Usr usr = (Usr)obj;
        if (usr != null)
        {
            return usr.Usr_name;
        }
        else
        {
            return string.Empty;
        }
    }
}
